/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.comfacauca.xmlc2.servlet;

import com.comfacauca.xmlc2.dao.ProyectosDAO;
import com.comfacauca.xmlc2.entidades.Requerimiento;
import com.comfacauca.xmlc2.servicio.ProyectosI;
import com.google.gson.Gson;
import com.sun.jmx.snmp.BerDecoder;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author sarita
 */
public class TraerAvanceServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    ProyectosI proyectosI = new ProyectosDAO();
    List subs = new ArrayList<Object>();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession();
        Integer idautor = (Integer) session.getAttribute("id");
        
        
        Object[] avance = {0,0,0,0,0,0};
        
        
        try{
        
            if("1".equals(request.getParameter("tipo"))||"2".equals(request.getParameter("tipo"))){
                subs = proyectosI.traerSubs(Integer.parseInt(request.getParameter("id")));    
                float avanceReq =0 ;
                int tiempoReq =0 ;
                int peso=0;
                
                for (Requerimiento m : (List<Requerimiento>) subs) {
                    Object avances[] = proyectosI.traerAvance(m.getId());
                    if(avances==null){continue;}                                            
                    
                    switch(m.getIdEtapa().getId()){
                        case 1:
                                peso = 20;
                            break;
                        case 2:
                                peso = 50;
                            break;
                        case 3:
                                peso = 20;
                            break;
                        case 4:
                                peso = 10;
                            break;
                    }
                    
                    avanceReq = avanceReq + (Float.parseFloat(avances[0].toString())*peso)/100; 
                    tiempoReq = tiempoReq + Integer.parseInt(avances[1].toString());
                    
                    //m.getIdResponsable().getIdEtapa().getId();
                }            
                Date now = new Date();                
                DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
                String s3 = df3.format(now);
                
                avance[0] = s3;
                
                avance[2] = avanceReq;
                avance[3] = "TODAS";
                avance[5] = tiempoReq;
                    
            }else{
               
                avance = proyectosI.traerAvance(Integer.parseInt(request.getParameter("id")),idautor);   
                
//                DateFormat df = DateFormat.getDateInstance();
//                
//                Date d = null;
//                
//                    try {
//                        d = df.parse(avance[0].toString());
//                    } catch (ParseException ex) {
//                        Logger.getLogger(TraerAvanceServlet.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                
//                DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
//                avance[0] = df3.format(d);
                                               
                if(!avance[5].toString().equals("null"))
                    avance[5] = proyectosI.traerAvanceTiempo(Integer.parseInt(request.getParameter("id")));
                
            }
        
        }catch(NullPointerException ne){
             ne.printStackTrace();
            }
        
        
        Gson gson = new Gson();        
        out.println(gson.toJson(avance));
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
